/**
 * 管理订单
 */
var v_my_order = new Vue({
	el: '#orderAdminPage',
	data: {
		title: '订单管理',
		needBack: false,
		needRefresh: false,
		navs: [{
			title: '代理商审核',
			icon: 'icon-tuandui',
			url: '/admin',
			active: '',
			badge: 0
		}, {
			title: '商品管理',
			icon: 'icon-shangpin',
			url: '/admin/goods',
			active: '',
			badge: 0
		}, {
			title: '订单管理',
			icon: 'icon-dingdan',
			url: '/admin/order',
			active: 'active',
			badge: 0
		}],
		show: false,
		searchMobileNo: '',
		unpaidOrders: [],
		deliveringOrders: [],
		orderNum:{},
		active: ''
	},
	created: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.showUnpaidOrders();
			this.loadOrderNum();
			this.count();
		},
		count: function() {
			axios.post('/agent/audit/count')
			.then(function(resp) {
				if (resp.data.success) {
					v_my_order.navs[0].badge = resp.data.message;
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		},
		logout: function() {
			self.location.href = '/logout';
		},
		showUnpaidOrders: function() {
			this.active = 'UNPAID';
			this.loadOrders(1, 'UNPAID', function(rows) {
				v_my_order.unpaidOrders = rows
				v_my_order.$forceUpdate();
			});
		},
		showDeliveringOrders: function() {
			this.active = 'DELIVERING';
			this.loadOrders(1, 'DELIVERING', function(rows) {
				v_my_order.deliveringOrders = rows
			});
		},
		goback: function() {
			// 返回
		},
		gotoPay: function(order) {
			
		},
		gotoDetail: function(order) {
			v_my_order_detail.order = order;
			v_my_order_detail.$nextTick(function() {
				$.router.load("#orderDetailPage");
			});
		},
		searchOrders: function() {
			if (this.searchMobileNo) {
				if (this.active == 'UNPAID') {
					this.showUnpaidOrders();
				} else {
					this.showDeliveringOrders();
				}
			} else {
				$.toast('请输入手机号码');
			}
		},
		loadOrders: function(pageNo, status, callback) {
			// 初始化推荐人
			var param = {
				page_pageNo: pageNo,
				page_pageSize: 10,
				page_sort: 'orderTime_desc',
				filter_EQ_status: status,
				'filter_EQ_receiver.phone': this.searchMobileNo
			};
			axios.post('/order/list', param)
			.then(function(resp) {
				if (resp.data.success) {
					$.each(resp.data.rows, function(i, row) {
						row.receiverAddress = row.province + row.city + row.region + row.address;
						if (row.status == 'UNPAID') {
							row.orderHeaderClass = 'alert-danger';
							row.canCancel = true;
						} else if (row.status == 'DELIVERING') {
							row.orderHeaderClass = 'alert-warning';
							row.canCancel = true;
						} else if (row.status == 'SUCCEED') {
							row.orderHeaderClass = 'alert-success';
							row.canCancel = false;
						} else if (row.status == 'CANCELLED' || row.status == 'CLOSE') {
							row.orderHeaderClass = 'alert-secondary';
							row.canCancel = false;
						} else {
							row.orderHeaderClass = 'alert-info';
							row.canCancel = false;
						}
					});
					callback && callback(resp.data.rows);
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		},
		loadOrderNum: function() {
			axios.post('/order/count')
			.then(function(resp) {
				if (resp.data.success) {
					v_my_order.orderNum = resp.data.message;
					var count = 0;
					$.each(resp.data.message, function(key, value) {
						count += Number(value);
					});
					v_my_order.navs[1].badge = count;
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		},
		confirmPaid: function(order) {
			$.modal({
				title: '确认订单已付款',
				text: '您是否已通过微信、支付宝、银行转账等方式收到了对方的款项？收到点“确认收款”，否则再想想',
				buttons: [{
					text: '确认收款',
					onClick: function() {
						axios.post('/order/confirm/paid/' + order.id)
						.then(function(resp) {
							if (resp.data.success) {
								$.toast('已收到收款确认请求，后台正在处理中……');
								setTimeout(function() {
									v_my_order.showUnpaidOrders();
									v_my_order.loadOrderNum();
								}, 2000);
							}
						})
						.catch(function (error) {
						    console.log(error);
						});
					}
				},{
					text: '再想想'
				}]
			});
		},
		shipping: function(order) {
			$.popup('div.popup-express');
			v_express.orderId = order.id;
		}
		
	}
});

var v_express = new Vue({
	el: '#expressPage',
	data: {
		orderId: '',
		logistics: {
			express: '圆通',
			waybillNo: ''
		}
	},
	methods: {
		shippingOrder: function(order) {
			if (!this.logistics.waybillNo) {
				$.toast('请输入运单号');
				return false;
			}
			axios.post('/order/shipping/' + this.orderId, this.logistics)
			.then(function(resp) {
				if (resp.data.success) {
					$.toast('已收到订单物流信息，后台正在处理中……');
					$.closeModal('div.popup-express');
					setTimeout(function() {
						v_my_order.showDeliveringOrders();
						v_my_order.loadOrderNum();
					}, 2000);
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		}
	}
});

var v_my_order_detail = new Vue({
	el: '#orderDetailPage',
	data: {
		title: '订单详情',
		needBack: true,
		needRefresh: false,
		order: {},
		orderTitles: [{
			orderNo: '订单号'
		},{
			orderTime_fmt: '下单时间'
		},{
			payChannel_fmt: '支付渠道'
		},{
			agentName: '购买代理商'
		},{
			receiver: '收货人 '
		},{
			phone: '收货人 电话'
		},{
			receiverAddress: '收货地址'
		},{
			express: '发货物流公司'
		},{
			waybillNo: '运单号'
		},{
			deliverTime: '发货时间'
		},{
			receiveTime: '收货时间'
		}]
	},
	methods: {
		goback: function() {
			
		}
	}
});